package com.itheima.day_02.homework02;


import java.util.Random;
import java.util.Scanner;

public class Test04 {
    public static void main(String[] args) {

        while(true) {
            String str = method();
            System.out.println("当前验证码:" + str );
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入:");
            String num = sc.next();
            if (num.length()!=4){
                System.out.println("验证失败");
            }else{
                if(str.equalsIgnoreCase(num)){
                    System.out.println("验证通过");
                    break;
                }else{
                    System.out.println("验证失败");
                }

            }

        }

    }

    public static String method(){
        Random ra = new Random();
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder(str);
        sb.append(str.toLowerCase());
        String num = "0123456789";
        sb.append(num);

        String st = sb.toString();
        char[] arr = st.toCharArray();
        StringBuilder ssb = new StringBuilder();
        for (int i=0;i<4;i++){
            int index = ra.nextInt(arr.length);
            ssb.append(arr[index]);
        }

        String s = ssb.toString();
        return s;
    }


}